package com.xmler.plugin.adbrocket.ui.renderer;

import com.xmler.plugin.adbrocket.bean.ProcessBean;

import javax.swing.*;
import java.awt.*;

public class ProcessComboBoxRenderer extends JLabel implements ListCellRenderer<ProcessBean> {

    @Override
    public Component getListCellRendererComponent(JList<? extends ProcessBean> list, ProcessBean value, int index, boolean isSelected, boolean cellHasFocus) {
        StringBuilder sb = new StringBuilder("<html>");
        if (value == null) {
            return this;
        }
        if (value.getPid() > 0) {
            int lastDot = value.getPackageName().lastIndexOf(".");
            sb.append("<font color=\"#aaaaaa\">").append(value.getPackageName().substring(0, lastDot + 1)).append("</font>");
            sb.append("<font color=\"#dddddd\">").append(value.getPackageName().substring(lastDot + 1)).append("</font>");
            sb.append("&nbsp;&nbsp;<font color=\"#888888\">(").append(value.getPid()).append(")</font>");
        } else {
            sb.append("<font color=\"#FF6347\">").append(value.getPackageName()).append("</font>");
        }
        sb.append("</html>");
        setText(sb.toString());
        return this;
    }
}
